//
//  CYCallTableCell.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/4/25.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCallTableCell.h"
#import "CYCallRecord.h"
#import "UIImageView+WebCache.h"
#import "CYABTool.h"
#import "CYEscape.h"
#import "CYDialTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"

@implementation CYCallTableCell

- (void)awakeFromNib {
    
    
}
- (void)setCallRecord:(CYCallRecord *)callRecord {
    _callRecord = callRecord;
    [self.callImageView setImage:[UIImage imageNamed:@"ic_contact_info_header"]];
    if (callRecord.callHistory) {
        
        //        self.phoneLabel.text = callRecord.callHistory.phone;
        if (callRecord.callHistory.name && ![callRecord.callHistory.name isEqualToString:@""]) {
            self.callnameLab.text = callRecord.callHistory.name;
            
            if (callRecord.thumbnailLocal) {
                self.callImageView.image = callRecord.thumbnailLocal;
            } else {
                [self imageFromServiceWithName:self.callnameLab.text];
            }
        } else {
            self.callnameLab.text = callRecord.callHistory.phone;
            if (!callRecord.callHistory.headImageName) {
                [self imageFromLocal];
            } else {
                [self.callImageView setImage:[UIImage imageNamed:callRecord.callHistory.headImageName]];
            }
        }
        
        if (callRecord.showTimeView) {//最近一次通话或者通话时长
            if (![callRecord.newlyCallDate isEqualToString:@""]) {//最近一次通话
                self.calltimeLab.text = callRecord.newlyCallDate;
            }else {//通话时长
                self.calltimeLab.text = callRecord.callDurationSum1;
            }
//            self.timeView.hidden = NO;
        } else {
//            self.timeView.hidden = YES;
            self.calltimeLab.text = @"";
        }
        
    } else if (callRecord.person) {
        self.callnameLab.text = callRecord.person.name;
        if (callRecord.person.person.thumbnail) {
            self.callImageView.image = callRecord.person.person.thumbnail;
        }
    }
    
}

/**
 *  随机匹配头像
 */
- (void)imageFromServiceWithName:(NSString *)name {
    CYPerson *cyperson = self.callRecord.person;
    if (cyperson && cyperson.person && !cyperson.person.thumbnail) {
        [CYABTool imageSelectOneWithParams:@{@"name": [CYEscape escapeWithString:name]} success:^(NSArray *defrs) {
            if (self.callRecord.callHistory.headImageName || self.callRecord.person.person.thumbnail) return;
            [self.callImageView sd_setImageWithURL:[NSURL URLWithString:defrs.firstObject] placeholderImage:[UIImage imageNamed:@"ic_contact_info_header"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                //保存图片到本地
                RHPerson *person = self.callRecord.person.person;
                if (!person.thumbnail) {
                    [person setImage:image];
                    [person save];
                }
            }];
            
        } failure:^(NSError *error) {
            
        }];
    }
}

- (void)imageFromLocal {
    int a = [CYDialTool randomNumForm:1 to:28];
    NSString *imageName = CYStringFormat(@"animol%d", a);
    UIImage *image = [UIImage imageNamed:imageName];
    self.callRecord.callHistory.headImageName = imageName;
    [self.callImageView setImage:image];
    [self.callRecord.callHistory update];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
